﻿using System.Web.Mvc;
using zeus.Core;
using SharpArch.Web.NHibernate;
using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;
using System.Collections.Generic;
using Webdiyer.WebControls.Mvc;
using System;
using zeus.Core.RepositoryInterfaces;
using System.Web.Security;
using System.Web;
using zeus.Web.Controllers.Helpers;

namespace zeus.Web.Controllers
{

    [HandleError]
    public class LoginController : Controller
    {
        #region Constructor
        private readonly IRepository<Usuario> usuarioRepository;
        private readonly IMenuRepository menuRepository;

        public LoginController(IRepository<Usuario> usuarioRepository,
            IMenuRepository menuRepository)
        {
            Check.Require(usuarioRepository != null, "usuarioRepository may not be null");
            this.usuarioRepository = usuarioRepository;

            Check.Require(menuRepository != null, "menuRepository may not be null");
            this.menuRepository = menuRepository;
        }

        public WorkingSession workingSession
        {
            get
            {
                if (Session["WorkingSession"] == null)
                    throw new SessionExpiredException();
                return Session["WorkingSession"] as WorkingSession;
            }
            set
            {
                Session["WorkingSession"] = value;
            }
        }
        #endregion

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Usuario usuario, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                // Validamos la información del usuario
                Dictionary<string, object> filtro = new Dictionary<string, object>();
                filtro.Add("Login", usuario.Login);
                filtro.Add("Clave", usuario.Clave);
                filtro.Add("Activo", true);

                Usuario usu = usuarioRepository.FindOne(filtro);
                
                if (usu != null)
                {
                    workingSession = new WorkingSession();
                    workingSession.Usuario = usu;

                    // Asigno el menu                    
                    workingSession.Menu = menuRepository.GetByRol(usu.Rol);
                    
                    FormsAuthentication.SetAuthCookie(usuario.Clave, true);
                    FormsAuthentication.SetAuthCookie(usuario.Login, true);
                    
                    //if (!String.IsNullOrEmpty(returnUrl))
                    //    return Redirect(returnUrl);
                    
                    //else                    
                        return RedirectToAction("Index", "Home");                    
                }
                else                
                    ModelState.AddModelError("", "El nombre de usuario o clave son incorrectos.");
                
            }

            return View(usuario);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

    }
}

